package com.alm.token.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.GlideApp;
import com.alm.token.R;
import com.alm.token.ui.activity.FundArealistActivity;
import com.alm.token.ui.activity.TurnOutActivity;
import com.alm.token.ui.data.bean.FundArealistResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.alm.token.ConStant.HOST;

/**
 * Created by en on 2019/4/23.
 */

public class CityAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<FundArealistResponse.DataBean> list;
    private Map<String, Integer> alphaIndexer;
    private String[] sections;
    private Context context;
    private boolean isZhongwen=true;

    public CityAdapter(Context context, List<FundArealistResponse.DataBean> list) {

        //赋值初始化
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.list = list;
        isZhongwen=LanguageUtils.getSetIndex() == 1;
        alphaIndexer = new HashMap<String, Integer>();
        if(list!=null){
            sections = new String[list.size()];

            //把相邻的相同的首字母放到一起,同时首个字母显示
            for (int i = 0; i < list.size(); i++) {
                String currentStr = DigitUtil.getPinYinFirst(isZhongwen?list.get(i).getCountrys():list.get(i).getYingwen());
                String previewStr = (i - 1) >= 0 ? DigitUtil.getPinYinFirst(isZhongwen?list.get(i - 1).getCountrys():list.get(i - 1).getYingwen())
                        : " ";
                if (!previewStr.equals(currentStr)) {//前一个首字母与当前首字母不同时加入HashMap中同时显示该字母
                    String name = DigitUtil.getPinYinFirst(isZhongwen?list.get(i).getCountrys():list.get(i).getYingwen());
                    alphaIndexer.put(name, i);
                    sections[i] = name;
                }
            }
        }


    }

    public Map<String, Integer> getCityMap() {
        return alphaIndexer;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_city, null);
            holder = new ViewHolder();
            holder.alpha = (TextView) convertView
                    .findViewById(R.id.item_city_alpha);
            holder.name = (TextView) convertView
                    .findViewById(R.id.item_city_name);
            holder.img_pic = (ImageView) convertView
                    .findViewById(R.id.item_city_pic);
            holder.rl_item = (RelativeLayout) convertView
                    .findViewById(R.id.rl_item);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        GlideApp.with(context).load(HOST+list.get(position).getPicture()).error(R.drawable.china).into(holder.img_pic );
        holder.name.setText(isZhongwen?list.get(position).getCountrys():list.get(position).getYingwen());
        String currentStr = DigitUtil.getPinYinFirst(isZhongwen?list.get(position).getCountrys():list.get(position).getYingwen());
        String previewStr = (position - 1) >= 0 ? DigitUtil.getPinYinFirst(isZhongwen?list.get(position - 1)
                .getCountrys():list.get(position - 1).getYingwen()) : " ";



        if (!previewStr.equals(currentStr)) {
            holder.alpha.setVisibility(View.VISIBLE);
            holder.alpha.setText(currentStr);
        } else {
            holder.alpha.setVisibility(View.GONE);
        }

        holder.rl_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((FundArealistActivity)context).setItemClick(list.get(position));
            }
        });
        return convertView;
    }

    private class ViewHolder {
        RelativeLayout rl_item;
        TextView alpha;
        TextView name;
        ImageView img_pic;
    }
}
